//
//  WDPDHeadView.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/7.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDPDHeadCell.h"
#import "WDProductDetail.h"
#import "WDProduct.h"

@interface WDPDHeadCell ()
@property (assign, nonatomic, getter=isFirstCome) BOOL firstCome;
/** 产品图标 */
@property(nonatomic, weak) UIImageView *iconView;
/** 产品名称 */
@property(nonatomic, weak) UILabel *nameLabel;
/** 车型 */
@property (nonatomic, weak) UIButton *carTypeLabel;
/** 申请人数 */
@property(nonatomic, weak) UILabel *applyPersonLabel;
/** 申请人数数字 */
@property(nonatomic, weak) UILabel *applyNumberLabel;
/** 通过率 */
@property(nonatomic, weak) UILabel *passRateLael;
/** 通过率数字 */
@property(nonatomic, weak) UILabel *passNumberLabel;
/** 特点 */
@property(nonatomic, strong) UILabel *introLabel;
/** 右边角 */
@property(nonatomic, weak) UIImageView *rightCornerView;

@end

@implementation WDPDHeadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.firstCome = YES;
        //添加子控件
        UIImageView *iconView = [[UIImageView alloc] init];
        [self addSubview:iconView];
        self.iconView = iconView;
        [iconView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(WDScale(16));
            make.width.equalTo(WDScale(112));
            make.height.equalTo(WDScale(80));
            make.top.equalTo(20);
        }];
        
        self.nameLabel = [self creatLabelWithFont:16 fontColor:ColorForRGB(0x4c4c4c)];
        [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconView.right).offset(38.5);
            make.top.equalTo(16);
            
        }];
        
        UIButton *carTypeLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        
        carTypeLabel.userInteractionEnabled = NO;
        
        carTypeLabel.backgroundColor = ColorForRGB(0xc7c7c7);
        [carTypeLabel setTitleColor:ColorForRGB(0xffffff) forState:UIControlStateNormal];
        carTypeLabel.titleLabel.font = [UIFont systemFontOfSize:12];
        carTypeLabel.layer.cornerRadius = 2;
        carTypeLabel.clipsToBounds = YES;
        [self addSubview:carTypeLabel];
        
        self.carTypeLabel = carTypeLabel;
        
        [carTypeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.nameLabel.bottom).offset(8.5);
            make.left.equalTo(self.nameLabel.left);
        }];
        
        carTypeLabel.contentEdgeInsets = UIEdgeInsetsMake(2, 5, 2, 5);
        
        self.applyPersonLabel = [self creatLabelWithFont:14 fontColor:ColorForRGB(0x6b6b6b)];
        self.applyPersonLabel.text = @"申请人数：";
        [self.applyPersonLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel.left);
            make.top.equalTo(self.carTypeLabel.bottom).offset(8);
        }];
        
        self.applyNumberLabel = [self creatLabelWithFont:14 fontColor:ColorForRGB(0xaaaaaa)];
        [self.applyNumberLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.applyPersonLabel);
            make.left.equalTo(self.applyPersonLabel.right);
        }];
        
        self.passRateLael = [self creatLabelWithFont:14 fontColor:ColorForRGB(0x6b6b6b)];
        self.passRateLael.text = @"通过率：";
        [self.passRateLael makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel.left);
            make.top.equalTo(self.applyPersonLabel.bottom).offset(8);
        }];
        
        self.passNumberLabel = [self creatLabelWithFont:14 fontColor:ColorForRGB(0xaaaaaa)];
        [self.passNumberLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.passRateLael.right);
            make.top.equalTo(self.passRateLael.top);
        }];
        
        UIImageView *rightCornerView = [[UIImageView alloc] init];
        rightCornerView.image = [UIImage imageNamed:@"Rectangle 2161"];
        [self.contentView addSubview:rightCornerView];
        self.rightCornerView = rightCornerView;
        [self.rightCornerView makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(0);
            make.width.height.equalTo(16);
        }];
        
    }
    return self;
}

+ (instancetype)pdHeadCellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"pdHeadCell";
    WDPDHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (UILabel *)creatLabelWithFont:(CGFloat)font fontColor:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:font];
    label.textColor = color;
    [self addSubview:label];
    
    return label;
}

- (void)setProductDetail:(WDProductDetail *)productDetail
{
    _productDetail = productDetail;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:productDetail.parent_logo]];
    
    if (self.isFirstCome)
    {
        NSArray *desc = [productDetail.special componentsSeparatedByString:@","];
        
        CGFloat descH = 21;
        CGFloat descW = 50;
        CGFloat marginX = 16;
        CGFloat marginY = 5;
        
        NSInteger cols = (WDScreenW - 166.5) / (descW + marginX);
        
        UILabel *tempLabel = nil;
        
        //强制布局
        [self layoutIfNeeded];
        
        for (int index = 0; index < desc.count; index++) {
            
            if (![desc[index] isEqualToString:@""])
            {
                //行
                NSInteger row = index / cols;
                //列
                NSInteger col = index % cols;
                
                CGFloat descX = CGRectGetMinX(self.nameLabel.frame) + col *(descW + marginX);
                
                CGFloat descY = CGRectGetMaxY(self.passRateLael.frame) + 16  + row * (descH + marginY);
                
                UILabel *descLabel = [self creatLabelWithFont:14 fontColor:ColorForRGB(0xfd5722)];
                descLabel.textAlignment = NSTextAlignmentCenter;
                descLabel.layer.borderWidth = 0.5;
                descLabel.layer.borderColor = WDNavSeleColor.CGColor;
                descLabel.layer.cornerRadius = 2;
                descLabel.clipsToBounds = YES;
                
                descLabel.frame = CGRectMake(descX, descY, descW, descH);
                descLabel.text = desc[index];
                
                tempLabel = descLabel;
            }
        }
        
        if (tempLabel == nil) {
            
            productDetail.cellH = CGRectGetMaxY(self.passRateLael.frame) + 16;
            
        } else {
            
            //给cell的高赋值
            productDetail.cellH = CGRectGetMaxY(tempLabel.frame) + 16;
        }
        
        self.firstCome = NO;
    }
    
}

- (void)setProduct:(WDProduct *)product
{
    _product = product;
    
    self.nameLabel.text = product.name;
    
    if ([product.car_type integerValue] == 0) {
        [self.carTypeLabel setTitle:@"新车" forState:UIControlStateNormal];
    } else {
        [self.carTypeLabel setTitle:@"二手车" forState:UIControlStateNormal];
    }
    
    self.applyNumberLabel.text = [NSString stringWithFormat:@"%zd", self.product.apply_count];
    
    self.passNumberLabel.text = [NSString stringWithFormat:@"%.2f%%", product.pass_rate];
    
}

@end
